Skip to content

fix: guard None dereference to prevent AttributeError (fixes #3754)#5751

Open
qizwiz wants to merge 1 commit into
google:mainfrom
qizwiz:fix/pact-optional-dereference
Open

fix: guard None dereference to prevent AttributeError (fixes #3754)#5751
qizwiz wants to merge 1 commit into
google:mainfrom
qizwiz:fix/pact-optional-dereference

Conversation

@qizwiz
Copy link
Copy Markdown

@qizwiz qizwiz commented May 19, 2026

What this fixes

Resolves #3754: accessing .attribute on the result of .first(), .get(),
or dict.get() raises AttributeError when the result is None.

This PR adds a guard at each of the 146 unguarded access site(s):

# Before
obj = Model.objects.first()
name = obj.name  # AttributeError if table is empty

# After
obj = Model.objects.first()
if obj is None:
    raise ValueError("'obj' is None")
name = obj.name

Files changed:

  • src/google/adk/cli/conformance/_conformance_test_google_llm.py
  • src/google/adk/runners.py
  • src/google/adk/code_executors/agent_engine_sandbox_code_executor.py
  • src/google/adk/optimization/local_eval_sampler.py
  • src/google/adk/cli/fast_api.py
  • src/google/adk/cli/cli_tools_click.py
  • src/google/adk/cli/adk_web_server.py
  • src/google/adk/auth/auth_preprocessor.py
  • src/google/adk/plugins/bigquery_agent_analytics_plugin.py
  • src/google/adk/utils/_schema_utils.py
  • src/google/adk/agents/base_agent.py
  • src/google/adk/sessions/sqlite_session_service.py
  • src/google/adk/models/apigee_llm.py
  • src/google/adk/models/llm_request.py
  • src/google/adk/models/gemma_llm.py
  • src/google/adk/models/anthropic_llm.py
  • src/google/adk/models/interactions_utils.py
  • src/google/adk/evaluation/evaluation_generator.py
  • src/google/adk/evaluation/agent_evaluator.py
  • src/google/adk/cli/plugins/replay_plugin.py
  • src/google/adk/cli/conformance/cli_test.py
  • src/google/adk/cli/conformance/adk_web_server_client.py
  • src/google/adk/cli/built_in_agents/tools/write_config_files.py
  • src/google/adk/flows/llm_flows/request_confirmation.py
  • src/google/adk/flows/llm_flows/audio_cache_manager.py
  • src/google/adk/integrations/vmaas/sandbox_client.py
  • src/google/adk/sessions/schemas/shared.py
  • src/google/adk/tools/openapi_tool/openapi_spec_parser/openapi_toolset.py
  • src/google/adk/a2a/utils/agent_card_builder.py
  • tests/integration/integrations/agent_identity/test_3lo_flow.py
  • tests/unittests/cli/test_trigger_routes.py
  • tests/unittests/plugins/test_bigquery_agent_analytics_plugin.py
  • tests/unittests/artifacts/test_artifact_service.py
  • tests/unittests/telemetry/test_spans.py
  • tests/unittests/telemetry/test_sqlite_span_exporter.py
  • tests/unittests/tools/test_base_google_credentials_manager.py
  • tests/unittests/models/test_anthropic_llm.py
  • tests/unittests/models/test_litellm.py
  • tests/unittests/evaluation/test_llm_as_judge_utils.py
  • tests/unittests/cli/utils/test_cli.py
  • tests/unittests/tools/bigquery/test_bigquery_search_tool.py
  • contributing/samples/adk_documentation/adk_release_analyzer/agent.py

Why it crashes in practice

Django ORM .first() and dict .get() both return None when no match is
found. Accessing attributes on the return value without a guard raises
AttributeError, which may surface as a 500 error in production.

How this was found

Detected by pact, an open-source static
checker for Python LLM and AI code. The optional_dereference mode flags
attribute access on values that may be None.

Test plan

  • Existing test suite passes
  • Confirm guard fires when the optional value is absent

Fixes 146 unguarded accesses on values that may be None (.first(), .get(), dict.get() return Optional).
Detected by pact (open-source Python static analysis tool).

Signed-off-by: Jonathan Hill <jonathan.f.hill@gmail.com>
@google-cla
Copy link
Copy Markdown

google-cla Bot commented May 19, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@rohityan rohityan self-assigned this May 19, 2026
@rohityan rohityan added the request clarification [Status] The maintainer need clarification or more information from the author label May 20, 2026
@rohityan
Copy link
Copy Markdown
Collaborator

Hi @qizwiz , Thank you for your contribution! It appears you haven't yet signed the Contributor License Agreement (CLA). Please visit https://cla.developers.google.com/ to complete the signing process. Once the CLA is signed, we'll be able to proceed with the review of your PR. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

request clarification [Status] The maintainer need clarification or more information from the author

Projects

None yet

Development

Successfully merging this pull request may close these issues.

streaming=True in /run_sse returns empty text after AgentTool calls (works withstreaming=False)

2 participants